home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / keyb / scncodtp.zip / SCANCODE.PAS < prev   
Pascal/Delphi Source File  |  1990-09-22  |  4KB  |  94 lines

  1. {==============================================================}
  2. {                         SCANCODE.PAS                         }
  3. {                    By Pat Anderson, Sysop                    }
  4. {          Pascal Alley, Fidonet 343/52, (206) 222-6224        }
  5. {                                                              }
  6. { Turbo Pascal's ReadKey shows ASCII codes placed in the key-  }
  7. { board buffer by the BIOS keyboard interrupt handler, Int $09.}
  8. { The keyboard actually generates two scan codes, a "make" and }
  9. { a "break" code for each press and release of a key.  This    }
  10. { program displays the codes the keyboard actually generates.  }
  11. { Highly recommended: Ohlsen & Stoker, Turbo Pascal Advanced   }
  12. { Techniques.  Writing your own keyboard interrupt service     }
  13. { routines opens the way to "event driven" programs by simply  }
  14. { setting a boolean variable in the keyboard ISR and checking  }
  15. { it in main program - much faster than polling for key presses}
  16. { with ReadKey.  It also can let you pop up a menu on the [5]  }
  17. { key on the numeric keypad, or the ScrollLock key - keys that }
  18. { the BIOS keyboard ISR and hence ReadKey don't recognize.     }
  19. { These are definitely tools that Pascal programmers should    }
  20. { have.  See sample KEYPAD_5.PAS unit for example.  This       }
  21. { could be expanded to a general purpose unit quite easily.    }
  22. { Enjoy!                                                       }
  23. {                                                              }
  24. {==============================================================}
  25.  
  26. program scancode;
  27.  
  28. uses
  29.   Crt,
  30.   Dos;
  31.  
  32. var
  33.   P, NewP, OldP : byte;
  34.   PChanged : boolean;
  35.   SaveInt09, OldExitProc : pointer;
  36.   IsMake : boolean;
  37.  
  38. {$F+}
  39. procedure NewInt09; interrupt;             { replace BIOS keyboard handler }
  40.   begin
  41.     P := Port [$60];                       { Read the value on the }
  42.                                            { keyboard port }
  43.     Inline ($E4/$61/$8A/$E0/$0C/$80/       { clean up as BIOS would }
  44.             $E6/$61/$86/$E0/$E6/$61/       { (from Ohlsen & Stoker, }
  45.             $B0/$20/$E6/$20);              { TP Advanced Techniques) }
  46.   end;
  47.  
  48. procedure MyExit;                          { restore BIOS handler on exit! }
  49.   begin
  50.     ExitProc := OldExitProc;
  51.     SetIntVec (9, SaveInt09);
  52.   end;
  53. {$F-}
  54.  
  55. begin
  56.   OldExitProc := ExitProc;                 { Install exit procedure to }
  57.   ExitProc := @MyExit;                     { restore old Int09 on exit }
  58.   GetIntVec (9, SaveInt09);                { Save old Int09 }
  59.   SetIntVec (9, @NewInt09);                { Install new Int09 }
  60.  
  61.   ClrScr;
  62.  
  63.   NewP := P;                                { get value on kbd port }
  64.   OldP := NewP;                             { save it }
  65.   Write ('Waiting for key press...');       { Some important info }
  66.   Write ('Press Break to exit program.');
  67.   IsMake := true;                           { first press is "make" code }
  68.  
  69.   repeat                                    { repeat forever }
  70.     NewP := P;                              { get value on port again }
  71.  
  72.     if P = 70 then                          { Break to terminate! }
  73.       Halt;
  74.  
  75.     if (NewP <> OldP) and (NewP > 0) then   { see if it is different }
  76.       PChanged := true                      { and set "changed" flag }
  77.     else                                    { accordingly }
  78.       PChanged := false;
  79.  
  80.     if (PChanged) then begin                { if the value has changed }
  81.       GotoXY (1,1); ClrEOL;                 { show if "make" or "break" }
  82.       if IsMake then
  83.         Write ('MAKE code (key pressed)')
  84.       else
  85.         Write ('BREAK code (key release)');
  86.       IsMake := not IsMake;                 { next code will be opposite! }
  87.  
  88.       GotoXY (1,2); ClrEOL;                 { display the code }
  89.       Write (NewP);
  90.       OldP := NewP;                         { and save the value for next }
  91.     end;                                    { comparison }
  92.   until 1 = 0;
  93. end.
  94.